package org.misty.blog.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

public class ServletUtils {
    public static ServletRequestAttributes getServletRequestAttributes() {
        // 原理: ThreadLocal !
        return (ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
    }

    public static HttpServletRequest getRequest() {
        return ServletUtils.getServletRequestAttributes().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return ServletUtils.getResponse();
    }
}
